import * as THREE from 'three';
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import {WebGLRenderer} from "three";

// 创建场景
const scene = new THREE.Scene();

// 创建相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 10, 20);

// 创建灯光
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);

const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(10, 10, 10);
scene.add(directionalLight);
const renderer = new WebGLRenderer();
// 创建控制器
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.screenSpacePanning = false;
controls.minDistance = 10;
controls.maxDistance = 50;
controls.maxPolarAngle = Math.PI / 2;

// 创建板面
const tableTopGeometry = new THREE.BoxGeometry(4, 0.2, 2);
const tableTopMaterial = new THREE.MeshStandardMaterial({ color: 0x964b00 });
const tableTop = new THREE.Mesh(tableTopGeometry, tableTopMaterial);

// 创建桌腿
const tableLegGeometry = new THREE.BoxGeometry(0.2, 1.5, 0.2);
const tableLegMaterial = new THREE.MeshStandardMaterial({ color: 0x964b00 });
const tableLeg1 = new THREE.Mesh(tableLegGeometry, tableLegMaterial);
const tableLeg2 = new THREE.Mesh(tableLegGeometry, tableLegMaterial);
const tableLeg3 = new THREE.Mesh(tableLegGeometry, tableLegMaterial);
const tableLeg4 = new THREE.Mesh(tableLegGeometry, tableLegMaterial);

// 组合桌面和桌腿
const table = new THREE.Group();
table.add(tableTop);
table.add(tableLeg1);
table.add(tableLeg2);
table.add(tableLeg3);
table.add(tableLeg4);

// 设置桌面和桌腿的位置
tableTop.position.set(0, 0.9, 0);
tableLeg1.position.set(-1.5, -0.6, -0.8);
tableLeg2.position.set(-1.5, -0.6, 0.8);
tableLeg3.position.set(1.5, -0.6, -0.8);

//在场景中创建两层，每层都包含三张桌子。


// 创建第一层
const firstFloor = new THREE.Group();
firstFloor.position.set(0, 0, 0);
scene.add(firstFloor);

// Create walls
const firstFloorWalls = new THREE.Mesh(
    new THREE.BoxGeometry(15, 5, 0.5),
    new THREE.MeshStandardMaterial({ color: 0xCCCCCC })
);
firstFloor.add(firstFloorWalls)


// 创建第一层的桌子
const table1 = table.clone();
const table2 = table.clone();
const table3 = table.clone();

table1.position.set(-5, 0, -1);
table2.position.set(0, 0, -1);
table3.position.set(5, 0, -1);

firstFloor.add(table1);
firstFloor.add(table2);
firstFloor.add(table3);

// 创建第二层
const secondFloor = new THREE.Group();
secondFloor.position.set(0, 2.5, 0);
scene.add(secondFloor);

// 创建第二层的桌子
const table4 = table.clone();
const table5 = table.clone();
const table6 = table.clone();

table4.position.set(-5, 0, -1);
table5.position.set(0, 0, -1);
table6.position.set(5, 0, -1);

secondFloor.add(table4);
secondFloor.add(table5);
secondFloor.add(table6);

// 创建渲染器
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 渲染循环
function animate() {
    requestAnimationFrame(animate);

    // 更新控制器
    controls.update();

    // 渲染场景
    renderer.render(scene, camera);
}

animate();

